package com.piepesken.mediscale.ui

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.google.accompanist.pager.ExperimentalPagerApi
import com.piepesken.mediscale.ui.drugs.DrugDetails
import com.piepesken.mediscale.ui.drugs.DrugsScreen
import com.piepesken.mediscale.ui.journal.JournalScreen
import com.piepesken.mediscale.ui.navigation.Screen
import com.piepesken.mediscale.ui.navigation.navigationScreens
import com.piepesken.mediscale.ui.overview.ChooseScaleScreen
import com.piepesken.mediscale.ui.overview.OverviewScreen
import com.piepesken.mediscale.ui.profile.ProfileScreen
import com.piepesken.mediscale.ui.theme.MediscaleTheme
import com.piepesken.mediscale.ui.weigh.WeightScreen
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MediscaleActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MediscaleApp()
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class, ExperimentalPagerApi::class)
@Composable
fun MediscaleApp() {
   MediscaleTheme {
       val navController = rememberNavController()

       Scaffold(
           modifier = Modifier.fillMaxSize(),
           bottomBar = {
               val navBackStackEntry by navController.currentBackStackEntryAsState()
               val currentDestination = navBackStackEntry?.destination

               NavigationBar {
                   navigationScreens.forEach {
                       screen -> NavigationBarItem(
                           icon = { Icon(
                               imageVector = screen.icon,
                               contentDescription = stringResource(id = screen.resourceId)
                           ) },
                           selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
                           onClick = {
                               navController.navigate(screen.route) {
                                   popUpTo(navController.graph.findStartDestination().id) {
                                       saveState = true
                                   }
                                   launchSingleTop = true
                                   restoreState = true
                               }
                           }
                       )
                   }
               }


           }
       ) { innerPadding ->
           NavHost(navController, startDestination = Screen.Overview.route, Modifier.padding(innerPadding)) {
               composable(Screen.Overview.route) { OverviewScreen(navController) }
               composable(Screen.Journal.route) { JournalScreen(navController) }
               composable(Screen.Drugs.route) { DrugsScreen(navController) }
               composable(Screen.Profile.route) { ProfileScreen(navController) }
               composable(
                   "drug/{drugId}",
                   arguments = listOf(navArgument("drugId") {
                       type = NavType.IntType
                   })) {
                   backStackEntry ->
                       DrugDetails(navController, backStackEntry.arguments?.getInt("drugId"))
               }
               composable("selectScale") {
                   ChooseScaleScreen(navController)
               }
               composable("weigh/{deviceId}",
                   arguments = listOf(navArgument("deviceId") {
                       type = NavType.StringType
                   })) { backStackEntry ->
                   WeightScreen(navController, backStackEntry.arguments?.getString("deviceId"))
               }
           }
       }
   } 
}